package connect4swt.gui.checkers;

import java.util.ArrayList;

import org.eclipse.swt.graphics.Point;

import connect4swt.gui.Piece;



//Before call this class, we have already garantee that 1st click gives us a valid piece and the 2nd click gives an empty position
// ATTENTION!! x is the line, y is the column
/**
 * @author  Joel Cordeiro
 */
public class PieceMove {

	
	/*
	 * The constructor. The parameters are the old piece, the new piece, the board, and the direction where the player is playing
	 */
	public PieceMove(CheckersPiece oldPiece, CheckersPiece newPiece, ArrayList<Piece> board, int direction)
	{
		this.actualPiece = oldPiece;
		this.movePiece = newPiece;		
		this.board = board;	
		this.direction = direction;
	}	
	
	/*
	 * Verify if it is a valid move
	 */
	public boolean verifyMove()
	{			
		if(verifyAdjacent(this.actualPiece, this.movePiece))
			return true;
		else if(verifyEat(this.actualPiece, this.movePiece))
		{
			return true;								
		}
							
		return false;
	}
	
	/*
	 * Verify if we can eat an opponent's piece
	 */
	public boolean verifyEat(CheckersPiece actualPiece, CheckersPiece movePiece)
	{		
	
		if (positionEatLeft(actualPiece, movePiece))
			return true;
		else if(positionEatRight(actualPiece, movePiece))
			return true;
				
		return false;
	}	
	
	/*
	 * Verify if the move is to eat a piece to the left
	 */
	public boolean positionEatLeft(CheckersPiece actualPiece, CheckersPiece movePiece)
	{
		if(movePiece.getBoardPositionX() == actualPiece.getBoardPositionX()-2  && (movePiece.getBoardPositionY() == actualPiece.getBoardPositionY()+(this.direction*2)))
		{
			CheckersPiece piece =  (CheckersPiece)getPiece(new Point(movePiece.getBoardPositionX()+1, movePiece.getBoardPositionY()-this.direction));
			if(piece!=null)
			{
				if(piece.imageType == CheckersPiece.ImageType.PIECE_BLUE || piece.imageType == CheckersPiece.ImageType.CHECKER_BLUE)
				{
					return true;
				}
			}
		}
		return false;
	}
	
	/*
	 * Verify if the move is to eat a piece to the right
	 */
	public boolean positionEatRight(CheckersPiece actualPiece, CheckersPiece movePiece)
	{
		if(movePiece.getBoardPositionX() == actualPiece.getBoardPositionX()+2  && movePiece.getBoardPositionY() == actualPiece.getBoardPositionY()+(this.direction*2))
		{
			CheckersPiece piece =  (CheckersPiece)getPiece(new Point(movePiece.getBoardPositionX()-1, movePiece.getBoardPositionY()-this.direction));
			if(piece!=null)
			{
				if(piece.imageType == CheckersPiece.ImageType.PIECE_BLUE || piece.imageType == CheckersPiece.ImageType.CHECKER_BLUE)
				{
					return true;
				}
			}
		}
		return false;
	}			
	
	/*
	 * Verify if the move is to go to a left adjacent position
	 */
	public boolean positionAdjacentLeft(CheckersPiece actualPiece, CheckersPiece movePiece)
	{
		if(movePiece.getBoardPositionX() == actualPiece.getBoardPositionX()-this.direction && movePiece.getBoardPositionY() == actualPiece.getBoardPositionY()+this.direction)
		{
			return true;
		}
		return false;
	}
	
	/*
	 * Verify if the move is to go to a right adjacent position
	 */
	public boolean positionAdjacentRight(CheckersPiece actualPiece, CheckersPiece movePiece)
	{
		if(movePiece.getBoardPositionX() == actualPiece.getBoardPositionX()+this.direction && movePiece.getBoardPositionY() == actualPiece.getBoardPositionY()+this.direction)
		{
			return true;
		} 
		return false;	
	}
	
	/*
	 * Verify if the piece is in a adjacent position
	 */
	public boolean verifyAdjacent(CheckersPiece actualPiece, CheckersPiece movePiece)
	{
		
		if (positionAdjacentLeft(actualPiece, movePiece))
			return true;
		else if(positionAdjacentRight(actualPiece, movePiece))
			return true;
				
		return false;									
	}	
	
	/*
	 * Gives as a piece of the position we give as a parameter
	 */
	protected Piece getPiece(Point position) {
		CheckersPiece tempPiece = null;
		
		for(Piece piece : board) {			
			
			if(position.x == piece.getBoardPositionX() && position.y == piece.getBoardPositionY()) {
				tempPiece = (CheckersPiece)piece;
			}
		}
		return tempPiece;
	}
	
	private CheckersPiece movePiece = null;
	private CheckersPiece actualPiece = null;	
	private ArrayList<Piece> board;	
	private int direction  = 0;
}
